package com.zl.controller;

import com.zl.pojo.EasyBuyUser;
import com.zl.service.EasyBuyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author ljm
 * @Date 2021/11/23 11:35
 */
@Controller
@RequestMapping("/user")
public class EasyBuyUserController {
    @Autowired
    private EasyBuyUserService us;
    @RequestMapping("/login")
    public String login(EasyBuyUser loginUser) {
        System.out.println(loginUser.getLoginName() + "  " + loginUser.getPassword());
        try{
            loginUser = us.login(loginUser);
            if(loginUser == null){
                System.out.println("登录失败！跳转到login.jsp");
                return "redirect:/Login.jsp";
            }else {
                System.out.println("登录成功！跳转到Index.jsp");
                return "redirect:/Index.jsp";
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/out")
    public String out(HttpSession session){
        session.invalidate();// 注销session
        return "redirect:/Index.jsp";
    }

    /*@RequestMapping("/regName")
    @ResponseBody//告诉springMVC需要把返回值转换为json格式
    public Map<String,Object> regName(String loginName){
        Map<String,Object> result = new HashMap<>();
        int num = us.regName(loginName);
        if (num > 0) {
            result.put("result",false);
        } else {
            result.put("result",true);
        }
        return result;
    }*/

    @RequestMapping("/regUser")// 请求路径被拦截
    public String regUser(EasyBuyUser easyBuyUser, HttpServletRequest req){
        EasyBuyUser easyBuyUser1 = us.queryEasyBuyUserByName(easyBuyUser.getLoginName());
        if (easyBuyUser1 == null) {
            us.regEasyBuyUser(easyBuyUser);
        } else {
            System.out.println("该用户已存在！注册失败");
            req.setAttribute("flag",false);
        }
        return "redirect:/Login.jsp";
    }
}

